Skip to content

Add Torch#1

Open
veehz wants to merge 128 commits intosource-academy:mainfrom
veehz:main
Open

Add Torch#1
veehz wants to merge 128 commits intosource-academy:mainfrom
veehz:main

Conversation

@veehz
Copy link
Copy Markdown

@veehz veehz commented Dec 19, 2025

Add Torch: a PyTorch-like machine learning library for Source Academy

This PR introduces Torch, a TypeScript implementation of a PyTorch-compatible ML library designed for educational use within the Source Academy platform.

Docs (also work in progress):

What's included

Core library (src/)

  • Tensor class with automatic differentiation, broadcasting, and zero-copy views via TensorStorage
  • 50+ tensor operations (unary, binary, reductions, shape manipulation, linear algebra) implemented as TorchFunction subclasses with correct forward and backward passes
  • nn.Module hierarchy: Linear, Conv1d/2d/3d, Sequential, and loss functions (MSELoss, BCELoss, CrossEntropyLoss)
  • SGD (with momentum and Nesterov) and Adam (with AMSGrad) optimizers
  • Seeded PRNG (Mulberry32) for reproducible tensor initialisation
  • no_grad() context, export_() for ATen-compatible graph introspection
  • Event bus exposing 8 event types (operation forward/backward, gradient accumulation) for external visualisation tools

Python bridge (examples/pyodide/bridge.py)

  • Wraps the JavaScript library for use from Python running in Pyodide, including operator overloading and context-manager support for no_grad()

Testing

  • scripts/generate_tests.py runs real PyTorch to produce numerical ground-truth fixtures
  • Generated tests cover forward passes and gradients for all operation classes
  • CI runs the full suite in both Node.js and a real browser via Playwright

Build targets

  • Node.js: CommonJS + ESM with TypeScript declarations
  • Browser: ESM module bundle
  • CDN: minified UMD bundle

@veehz veehz marked this pull request as ready for review March 22, 2026 13:46
@veehz veehz changed the title MVP Add Torch Mar 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant